package com.itheima.filter;

import com.alibaba.fastjson.JSONObject;
import com.itheima.pojo.Result;
import com.itheima.utlis.JwtUtils;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.io.IOException;

@Slf4j
@WebFilter(urlPatterns = "/*")
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;
        //获取请求url
        String url = req.getRequestURI().toString();
        log.info("请求的url，{}",url);

        //判断url中是否包含login。如果包含，说明是登录操作，放行
        if (url.contains("login")){
            log.info("登录操作，放行---");
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }


        //获取请求头的令牌（token）
        String jwt = req.getHeader("token");

        //判断令牌是否存在，如果不存在，返回错误结果（未登录）
        if(!StringUtils.hasLength(jwt)){
            log.info("请求头token为空，返回未登录信息");

            Result error = Result.error("NOT_LOGIN");
            //手动转换对象为JSON
            String notLogin = JSONObject.toJSONString(error);
            servletResponse.getWriter().write(notLogin);
            return;
        }
        else {

            //解析token，如果解析失败，返回错误结果（未登录）

            try {
                JwtUtils.parseJWT(jwt);
            } catch (Exception e) { // 解析失败
                e.printStackTrace();
                log.info("解析失败，返回未登录信息");
                Result error = Result.error("NOT_LOGIN");
                //手动转换对象为JSON
                String notLogin = JSONObject.toJSONString(error);
                servletResponse.getWriter().write(notLogin);
                return;
            }

            //放行
            log.info("令牌合法，放行");
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }
}
